import { Component, OnInit, EventEmitter } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { Subscription } from "rxjs/Subscription";
import { CmHttp } from "http-module";
import { UserToken, DEFAULT_VERIFY, OPERATE_BUY, OPERATE_SELL } from "vtrade";

export interface BusinessDetail {
  exchange_type;
  stock_account;
  stock_code;
  entrust_bs;
  business_price: number;
  business_amount;
  business_time;
  real_type;
  real_status;
  entrust_no;
  business_balance;
  stock_name;
  business_no;
  position_str;
  occur_amount?;
}

const TABLE_HEADERS = [
  {
    "label": "成交时间",
    "key": "business_time"
  },
  {
    "label": "委托编号",
    "key": "entrust_no"
  },
  {
    "label": "证券名称",
    "key": "stock_name"
  },
  {
    "label": "证券代码",
    "key": "stock_code"
  },
  {
    "label": "买卖方向",
    "key": "entrust_bs"
  },
  {
    "label": "成交价格",
    "key": "business_price"
  },
  {
    "label": "成交数量",
    "key": "business_amount"
  },
  {
    "label": "成交金额",
    "key": "business_balance",
  }
];

@Component({
  // moduleId: module.id,
  selector: "business-detail",
  templateUrl: "business-detail.component.html",
  styleUrls: [
    "business-detail.component.less"
  ],
  inputs: [
    "dataSource"
  ],
  outputs: [
  ]
})
export class BusinessDetailComponent implements OnInit {
  mainSubscription: Subscription;
  tableHeaders = TABLE_HEADERS;
  dataSource: Observable<BusinessDetail[]>;
  dataList: BusinessDetail[];
  positionClick = new EventEmitter();

  constructor() { }

  ngOnInit() {
    this.mainSubscription = this.dataSource.subscribe(
      res => this.dataList = res,
      err => this.dataList = []
    );
  }

  ngOnDestroy() {
    if (this.mainSubscription) {
      this.mainSubscription.unsubscribe();
    }
  }

  trackBySymbol(symbol: BusinessDetail) {
    return symbol.stock_code;
  }

  handleOperate(entry: BusinessDetail, isSell?: boolean) {
    console.debug("Stock Position Emit: ", entry, " isSell: ", isSell);
    this.positionClick.next({
      stock: entry,
      isSell
    });
  }

  getEntryClass(entry: BusinessDetail) {
    return {
      // "color-red": entry.income_balance >= 0,
      // "color-green": entry.income_balance < 0
    };
  }

}